package com.qf.text;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.mybatis.UserDao;
import com.qf.pojo.Linkman;
import com.qf.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class rwerew {
    private SqlSession sqlSession;
    @Before
    public void before() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
         sqlSession = factory.openSession();
    }
    //查询数据库总条数
    @Test
    public void findUser() throws IOException {
        //1. 加载配置文件,获取字节流
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        //2. 构建工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        //3. 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //4. 获取到UserMapper接口的代理对象
        UserDao userMapper = sqlSession.getMapper(UserDao.class);

        //5. 执行方法
        Long count = userMapper.findUser();
        System.out.println(count);
    }
    //查询一条user数据
    @Test
    public void selectUser() throws IOException {
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        User user = userMapper.selectUser();
        System.out.println(user.toString());
    }
    //查询一条带参数的user数据
    @Test
    public void selectUserC() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = userDao.selectUserC(1);
        System.out.println(user.toString());

    }
    //查询所有数据并执行的分页
    @Test
    public void findAll() throws IOException {
        //加载配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        //工厂类来获取配置信息
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        //用工厂中
        SqlSession sqlSession = factory.openSession();
        // 每页显示两条,分两页
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        //前端传过来的数据
        int page = 2;
        int size = 2;
        //1. 指定分页信息
         // 切记  必须在查询之前执行
        PageHelper.startPage(page,size);
        //2. 分页查询
        List<User> list = userMapper.findAll();    // 2个查询
        //3. 封装数据
        PageInfo<User> pageInfo = new PageInfo<>(list);

        System.out.println(pageInfo);
    }
    @Test
    //查询对应name和age的数据(了解，以后千万别用)
    public void findNameAndAgeSum() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> list = mapper.findNameAndAgeSum("张三", 19);
        System.out.println(list);


    }
    @Test
    //注解的方式查询对应name和age的数据
    public void findNameAndAgeSumZJ() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> list = mapper.findNameAndAgeSumZJ("李四", 20);
        for (User user:list) {
            System.out.println(user);
        }
    }

    @Test
    //map的方式查询对应的name和age的数据
    public void findNameAndAgeSumMAP() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //一个参数对应一个值，切记，键是别名，值是传的值
        Map<String,Object> map=new HashMap<>();
        map.put("name","王五");
        map.put("age",21);
        List<User> nameAadAgeSumMap = mapper.findNameAadAgeSumMap(map);
        for (User user:nameAadAgeSumMap){
            System.out.println(user);
        }
    }

    @Test
    //用Pojo的方式查询对应的name和age的数据
    public void findNameAndAgeSumPojo() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user=new User(0,"赵六", 22);
        List<User> pojo = mapper.findNameAadAgeSumPojo(user);
        for (User user1:pojo){
            System.out.println(user1);
        }
    }

    @Test
    //模糊查询
    public void findName(){
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> users = mapper.findName("张三");
        for (User user:users){
            System.out.println(user);
        }
    }
    @Test
    //根据条件删除数据
    public void deleteUser(){
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        int i = mapper.deleteUser(1);
        System.out.println(i);
    }
    @Test
    public void UpdateUser(){
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user=new User(2,"小康",99);
        int i = mapper.UpdateUser(user);
        sqlSession.commit();
        System.out.println(i);
    }
    @Test
//    主键回填
    public void InsertUser(){
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user=new User(0,"王小康",80);
        int i = mapper.InsertUser(user);
        System.out.println(i);
        System.out.println(user);
        sqlSession.commit();
    }



    //ORM无法映射用resurtmap
    @Test
    public void selectUserMap() throws IOException {
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        User user = userMapper.selectUser();
        System.out.println(user.toString());
    }
    @Test
    public void findIdAndCid(){
        UserDao userDao=sqlSession.getMapper(UserDao.class);
        List<Linkman> linkmen =userDao.findIdAndCid();
        for (Linkman linkman:linkmen){
            System.out.println(linkman);
        }
    }
}
